package service

import (
	"bugManagementPlatform/pojo"
	"bugManagementPlatform/utils"
	"bugManagementPlatform/variable"
	"gorm.io/gorm"
	"strconv"
	"time"
)

type BugManagementService struct {
	worker *utils.Worker
}

func NewBugManagementService() *BugManagementService {
	return &BugManagementService{
		utils.NewWorkerNovel(),
	}
}

func (bugManagementService *BugManagementService) queryProjectCaseAll(projectId int64) []*pojo.Case {

	var caseList []*pojo.Case
	variable.Db.Where("project_id = ?", projectId).Find(&caseList)

	for _, c := range caseList {
		variable.Db.Raw("select f.id, f.name, f.mold, f.mold_name, cf.value, cf.created_at, cf.updated_at \n" +
			"from field f inner join case_field cf on f.id = cf.field_id where cf.case_id = ?", c.Id).Find(&c.ShowFieldList)
	}

	return caseList
}

func (bugManagementService *BugManagementService) AddCase(c *pojo.Case) map[string]interface{} {

	c.Id = bugManagementService.worker.GetId()
	variable.Db.Create(c)

	for _, sf := range c.ShowFieldList {
		var cf = pojo.CaseField{
			Id:        bugManagementService.worker.GetId(),
			CaseId:    c.Id,
			FieldId:   sf.Id,
			Value:     sf.Value,
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		}
		variable.Db.Create(&cf)
	}

	return map[string]interface{}{"code":200, "message":"新增成功"}
}

func (bugManagementService *BugManagementService) AddProject(p *pojo.Project) map[string]interface{} {

	p.Id = bugManagementService.worker.GetId()
	variable.Db.Create(p)

	for _, f := range p.FieldList {
		var pf = pojo.ProjectField{
			Id:        bugManagementService.worker.GetId(),
			FieldId:   f.Id,
			ProjectId: p.Id,
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		}
		variable.Db.Create(&pf)
	}

	return map[string]interface{}{"code":200, "message":"新增成功"}
}

func (bugManagementService *BugManagementService) QueryProjectFiled(projectId int64) []*pojo.Field {

	var fieldList []*pojo.Field
	variable.Db.Table("field").
		Joins("JOIN project_field ON project_field.field_id = field.id and project_field.project_id = ?", projectId).
		Find(&fieldList)

	return fieldList
}

func (bugManagementService *BugManagementService) QueryFiledAll(pageNo int, pegeSize int, id int64, name, moldName, mold, startTime, endTime string) ([]*pojo.ResField, int) {

	var resFieldList []*pojo.ResField

	var count int64
	variable.Db.Model(&pojo.Field{}).Count(&count)

	var gs = variable.Db.Session(&gorm.Session{})

	if name != ""{
		gs = gs.Where("name like ?", "%"+name+"%")
	}

	if mold != ""{
		gs = gs.Where("mold = ?", mold)
	}

	if moldName != ""{
		gs = gs.Where("mold_name like ?", "%"+moldName+"%")
	}

	if startTime != ""{
		gs = gs.Where("created_at >= ?", startTime)
	}

	if endTime != ""{
		gs = gs.Where("created_at <= ?", endTime)
	}

	var fieldList []*pojo.Field
	if id != 0 {
		gs.Limit(pegeSize).Offset((pageNo-1)*pegeSize).Where("id", id).Find(&fieldList)
	}else {
		gs.Limit(pegeSize).Offset((pageNo-1)*pegeSize).Order("updated_at desc").Find(&fieldList)
	}
	for _, f := range fieldList {
		var rf = pojo.ResField{
			Id:        strconv.FormatInt(f.Id,10),
			Name:      f.Name,
			Mold:      f.Mold,
			MoldName:  f.MoldName,
			Picture:   f.Picture,
			CreatedAt: f.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdatedAt: f.UpdatedAt.Format("2006-01-02 15:04:05"),
		}
		resFieldList = append(resFieldList, &rf)
	}

	total, _ := strconv.Atoi(strconv.FormatInt(count,10))

	return resFieldList, total
}

//func (bugManagementService *BugManagementService) AddFiled(name string, mold int, moldName string, picture string) {
//
//	filed := pojo.Field{
//		Id:         variable.W.GetId(),
//		Name:       name,
//		Mold:       mold,
//		MoldName:   moldName,
//		Picture: 	picture,
//	}
//	variable.Db.Create(&filed)
//}

func (bugManagementService *BugManagementService) AddFiled(tx *gorm.DB, params ...interface{}) (interface{}, error){

	filed := pojo.Field{
		Id:         variable.W.GetId(),
		Name:       params[0].(string),
		Mold:       params[1].(int),
		MoldName:   params[2].(string),
		Picture: 	params[3].(string),
	}
	err := tx.Create(&filed).Error
	if err != nil{
		return map[string]interface{}{"message": "失败","code":400}, err
	}

	return map[string]interface{}{"message": "成功","code":200}, nil
}

func (bugManagementService *BugManagementService) UpdateFiled(filed *pojo.ResField) {

	//mold, _ := strconv.Atoi(filed.Mold)

	variable.Db.Model(&pojo.Field{}).Where("id", filed.Id).Updates(&pojo.Field{
		Name:       filed.Name,
		Mold:       filed.Mold,
		MoldName:   filed.MoldName,
		Picture: 	filed.Picture,
	})

}

func (bugManagementService *BugManagementService) DeleteFiledById(id int64) {

	variable.Db.Delete(&pojo.Field{}, id)
}


func (bugManagementService *BugManagementService) ViewReportById(id int64) string {

	var tr pojo.TestReport
	tr.Id = id
	variable.Db.First(&tr)

	return tr.FileName
}






